package info.magnolia.blossom.sample.cv;

import info.magnolia.blossom.sample.cv.model.CurriculumVitae;
import info.magnolia.blossom.sample.cv.service.CVService;
import info.magnolia.module.blossom.annotation.TabFactory;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.annotation.TemplateDescription;
import info.magnolia.module.blossom.dialog.TabBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: peterbalazs
 * Date: 2/18/13
 * Time: 1:03 PM
 * To change this template use File | Settings | File Templates.
 */
@Template(title = "Personal Details", id = "blossomSample:components/personaldetails")
@TemplateDescription("Personal Details on the CV")
@Controller
public class PersonalDetailsComponent {

    private static final String ERROR_MSG = "errorMsg";
    private static final String ERROR_PAGE_PATH = "components/error.jsp";

    @Autowired
    private CVService cvService;

    @RequestMapping("/cvtemplate/personaldetails")
    public String render(final ModelMap model, final HttpServletRequest request) {
        final String id = request.getParameter(ListOfCVs.PARAM_CV_ID);
        final CurriculumVitae cv = cvService.getCV(id);
        if (cv == null) {
            model.put(ERROR_MSG, "There is nothing to view");
            return ERROR_PAGE_PATH;
        }
        model.put("cv", cv);
        return "components/personalDetails.jsp";
    }

//    @RequestMapping("/cvtemplate/pictures/{cvId}")
//    @ResponseBody
//    public byte[] getCVImage(@PathVariable String cvId) {
//        final CurriculumVitae cv = cvService.getCV(cvId);
//        if (cv != null) {
//            final byte[] picData = cv.getPictureFile();
//            if (picData != null && picData.length > 0) {
//                return cv.getPictureFile();
//            }
//        }
//        // TODO return "no-image" image
//        return "no picture".getBytes();
//    }

    @TabFactory("Settings")
    public void settingsTab(final TabBuilder tab) {
        tab.addUuidLink("editLink", "Edit Page Link", "Browse and select the appropriate page for editing a CV");
    }
}
